-- Viral Resistance - T-Engine 4 Module
-- Copyright (C) 2010 Mikolai Fajer
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- Mikolai Fajer "yufra"
-- mfajer@gmail.com

require "engine.class"
local Mouse = require "engine.Mouse"

module(..., package.seeall, class.make)

local TOOLTIPS = {}
TOOLTIPS.LIFE = [[#GOLD#Life#LAST#
This is your life force, when you take damage this is reduced more and more.
If it reaches below zero you die.
Death is usualy permanent so beware!]]
TOOLTIPS.VIRAL = [[#GOLD#Viral Load#LAST#
This is amount of virus in you.
If it reaches 100 you succumb to the virus and turn into a zombie.
Some items and talents can help keep the virus at bay.]]
TOOLTIPS.STAMINA = [[#GOLD#Stamina#LAST#
Stamina represents your physical fatigue. Each physical ability used reduces it.
It regenerates slowly over time or when resting.]]

function _M:init(x, y, w, h, bgcolor)
	self.display_x = x
	self.display_y = y
	self.w, self.h = w, h
	self.bgcolor = bgcolor
	self.font = core.display.newFont("/data/font/VeraMono.ttf", 14)
	self.surface = core.display.newSurface(w, h)
	self.mouse = Mouse.new()
	self:resize(x, y, w, h)
end

--- Resize the display area
function _M:resize(x, y, w, h)
	self.display_x, self.display_y = x, y
	self.mouse.delegate_offset_x = x
	self.mouse.delegate_offset_y = y
	self.w, self.h = w, h
	self.font_h = self.font:lineSkip()
	self.font_w = self.font:size(" ")
	self.bars_x = self.font_w * 9
	self.bars_w = self.w - self.bars_x - 5
	self.surface = core.display.newSurface(w, h)
	self.surface = core.display.newSurface(w, h)
	self.texture, self.texture_w, self.texture_h = self.surface:glTexture()
end

function _M:mouseTooltip(text, _, _, _, w, h, x, y)
	self.mouse:registerZone(x, y, w, h, function(button) game.tooltip_x, game.tooltip_y = 1, 1; game.tooltip:displayAtMap(nil, nil, game.w, game.h, text) end)
end

-- Displays the stats
function _M:display()
	local player = game.player
	if not player or not player.changed then return self.surface end

	self.mouse:reset()
	local s = self.surface
	
	s:erase(self.bgcolor[1], self.bgcolor[2], self.bgcolor[3])

	local cur_exp, max_exp = player.exp, player:getExpChart(player.level+1)

	local h = 6
	local x = 2
	s:drawColorStringBlended(self.font, "Level: #00ff00#"..player.level, 0, h, 255, 255, 255) h = h + self.font_h
	s:drawColorStringBlended(self.font, ("Exp:  #00ff00#%2d%%"):format(100 * cur_exp / max_exp), 0, h, 255, 255, 255) h = h + self.font_h

	h = h + self.font_h

	s:erase(colors.VERY_DARK_RED.r, colors.VERY_DARK_RED.g, colors.VERY_DARK_RED.b, 255, self.bars_x, h, self.bars_w, self.font_h)
	s:erase(colors.DARK_RED.r, colors.DARK_RED.g, colors.DARK_RED.b, 255, self.bars_x, h, self.bars_w * player.life / player.max_life, self.font_h)
	self:mouseTooltip(TOOLTIPS.LIFE, s:drawColorStringBlended(self.font, ("#c00000#Life:    #ffffff#%d/%d"):format(player.life, player.max_life), 0, h, 255, 255, 255))
	h = h + self.font_h

	s:erase(0xff / 6, 0xcc / 6, 0x80 / 6, 255, self.bars_x, h, self.bars_w, self.font_h)
	s:erase(0xff / 3, 0xcc / 3, 0x80 / 3, 255, self.bars_x, h, self.bars_w * player:getStamina() / player:getMaxStamina(), self.font_h)
	self:mouseTooltip(TOOLTIPS.STAMINA, s:drawColorStringBlended(self.font, ("#ffcc80#Stamina: #ffffff#%d/%d"):format(player:getStamina(), player:getMaxStamina()), 0, h, 255, 255, 255))
	h = h + self.font_h

	s:erase(colors.DARK_GREEN.r, colors.DARK_GREEN.g, colors.DARK_GREEN.b, 255, self.bars_x, h, self.bars_w, self.font_h)
	s:erase(colors.LIGHT_GREEN.r, colors.LIGHT_GREEN.g, colors.LIGHT_GREEN.b, 255, self.bars_x, h, self.bars_w * player.vload / player.max_vload, self.font_h)
	self:mouseTooltip(TOOLTIPS.VIRAL, s:drawColorStringBlended(self.font, ("#c00000#Virus:    #ffffff#%d/%d"):format(player.vload, player.max_vload), 0, h, 255, 255, 255))
	h = h + self.font_h
	
	if savefile_pipe.saving then
		h = h + self.font_h
		s:drawColorStringBlended(self.font, "#YELLOW#Saving game...", 0, h, 255, 255, 255)
		h = h + self.font_h
	end
	
	h = h + self.font_h
	for tid, act in pairs(player.sustain_talents) do
		if act then
			s:drawColorStringBlended(self.font, ("#LIGHT_GREEN#%s"):format(player:getTalentFromId(tid).name), x, h, 255, 255, 255)
			h = h + self.font_h
		end
	end
	for eff_id, p in pairs(player.tmp) do
		local e = player.tempeffect_def[eff_id]
		if e.status == "detrimental" then
			s:drawColorStringBlended(self.font, ("#LIGHT_RED#%s"):format(e.desc), 0, h, 255, 255, 255)
			h = h + self.font_h
		else
			s:drawColorStringBlended(self.font, ("#LIGHT_GREEN#%s"):format(e.desc), 0, h, 255, 255, 255)
			h = h + self.font_h
		end
	end

	s:updateTexture(self.texture)
	return s
end

function _M:toScreen()
	self:display()
	self.texture:toScreenFull(self.display_x, self.display_y, self.w, self.h, self.texture_w, self.texture_h)
end